<template>
	<view class="change">
		<Navbar>安全中心</Navbar>
		<scroll-view scroll-y class="scroll_view">
			<view class="content">
				<view class="title">
					<view>
						<image :src="`${baseImageUrl}/common/zw.png`"></image>
						<text>设置密码</text>
						<image :src="`${baseImageUrl}/common/zw.png`"></image>
					</view>
				</view>
				<view class="item">
					<view class="key">请输入账号</view>
					<input type="text" placeholder="请输入手机号" disabled class="input" v-model="show_phone">
					<view class="input_code">
						<input type="text" placeholder="请输入验证码" autocomplete="off" v-model="code">
						<view class="get_code" v-if="isTimer">{{time}}s</view>
						<view class="get_code" v-else @click="getCode">获取验证码</view>
					</view>
				</view>
				<view class="item">
					<view class="key">设置新密码</view>
					<input type="text" password placeholder="设置新密码" v-model="password" class="input">
				</view>
				<view class="item">
					<view class="key">确认新密码</view>
					<input type="text" password placeholder="确认新密码" v-model="confirm_password" class="input">
				</view>
				<view class="btn" @click="submit">确认</view>
			</view>
		</scroll-view>
	</view>
</template>

<script>
	import {
		smsSend,
		userEditPassword
	} from '@/request/api.js';
	// import {
	// 	captchaAppId
	// } from "@/utils/config";
	export default {
		data() {
			return {
				type: "",

				phone: "",
				show_phone: "",
				code: "",

				password: "",
				confirm_password: ""
			}
		},
		onLoad(option) {
			this.type = option.type;
			this.phone = this.$store.state.info.phone;
			this.show_phone = this.$store.state.info.show_phone;
		},
		onUnload() {
			clearInterval(this.timer);
		},
		methods: {
			async getCode() {
				if (!this.phone) {
					this.$u.toast("请输入手机号");
					return false;
				};
				// const captcha = new TencentCaptcha(captchaAppId, async (res) => {
				// 	if (res.ret !== 0) return false;
				// 	let {
				// 		randstr,
				// 		ticket
				// 	} = res;
				this.isTimer = true;
				this.timer = setInterval(() => {
					this.time--
					if (this.time < 0) {
						this.isTimer = false;
						this.time = 60;
						clearInterval(this.timer);
					};
				}, 1000);
				await smsSend({
					phone: this.phone,
					scene: "password"
				});
				this.$u.toast("验证码已发送,请查看");
				// }, {});
				// captcha.show();
			},
			async submit() {
				await userEditPassword({
					field: this.type,
					code: this.code,
					password: this.password,
					confirm_password: this.confirm_password
				});
				this.$u.toast("修改成功");
				this.password = this.confirm_password = "";
			}
		}
	}
</script>

<style scoped lang="scss">
	.change {
		height: 100vh;
		background: #29170D;
		overflow: hidden;

		.scroll_view {
			height: calc(100vh - #{$navbarHeight});
			padding-top: 48rpx;
			box-sizing: border-box;

			.content {
				width: 100%;
				height: 800rpx;
				padding: 32rpx 75rpx 40rpx;
				box-sizing: border-box;
				background: url(#{$baseImageUrl}/bg/750_800.png) no-repeat;
				background-size: 100% 100%;
				display: flex;
				flex-direction: column;
				justify-content: space-between;
				align-items: center;

				.title {
					display: flex;
					justify-content: center;
					align-items: center;

					view {
						height: 30rpx;
						display: flex;
						align-items: center;

						image {
							width: 30rpx;
							height: 30rpx;
						}

						text {
							color: #494338;
							font-size: 42rpx;
							font-weight: 400;
							font-family: LiSu;
							margin: 0 10rpx;
						}
					}
				}

				.item {
					width: 100%;

					.key {
						color: #494338;
						font-size: 32rpx;
						font-weight: 400;
						font-family: SimSun;
						margin-bottom: 20rpx;
					}

					.input {
						width: 100%;
						height: 80rpx;
						border-radius: 10rpx;
						padding-left: 45rpx;
						box-sizing: border-box;
						box-shadow: 2rpx -1px 10rpx 0px rgba(3, 0, 0, 0.2);
						background: #C0B5B6;
					}

					.input_code {
						height: 80rpx;
						margin-top: 40rpx;
						border-radius: 10rpx;
						padding-left: 45rpx;
						padding-right: 36rpx;
						box-sizing: border-box;
						box-shadow: 2rpx -1px 10rpx 0px rgba(3, 0, 0, 0.2);
						background: #C0B5B6;
						display: flex;
						align-items: center;

						input {
							flex: 1;
							height: 80rpx;
						}

						.get_code {
							color: #FC2B1D;
							font-size: 28rpx;
							font-weight: 400;
							font-family: SimSun;
						}
					}
				}

				.btn {
					width: 204rpx;
					height: 74rpx;
					line-height: 74rpx;
					text-align: center;
					color: #FFFFFF;
					font-size: 30rpx;
					font-weight: 400;
					font-family: SimSun;
					background: url(#{$baseImageUrl}/common/btn.png) no-repeat;
					background-size: 100% 100%;
				}
			}
		}
	}
</style>